<?php
namespace app\index\model;

use think\Model;

class Category extends Model
{
    //方法定义关联
    public function article(){
        return $this->hasMany('Article');
    }

    /**
     * 获取列表
     * @param array $where
     * @param array $whereAssoc 关联条件
     * @return array|\PDOStatement|string|\think\Collection
     */
    public function getDetail($where=[], $whereAssoc=[]){
        $object = $this->where($where);
        //若关联条件存在
        foreach ($whereAssoc as $key => $value){
            $object = $object->hasWhere($key, $value);
        }
        return $object->select();
    }

    /**
     * 获取（某个分类的）详情
     * @param $id
     * @param array $where
     * @return array|null|\PDOStatement|string|Model
     */
    public function getDetailById($id, $where=[]){
        $where[] = ['id', '=', $id];
        return $this->where($where)->find();
    }

    /**
     * 判断是否存在子节点
     * @param $id
     * @return bool
     */
    public function hasSonNode($id){
        $sonNode = $this->where('pid', '=', $id)->find();
        return $sonNode? true: false;
    }

}